
//var hash_combine = require('build/Release/hash_combine');
const ecc = require('eosjs-ecc');
const crypto = require('crypto');
const bs58 = require('bs58');


//hash code 
//from :http://cognitect.github.io/transit-js/files/src_com_cognitect_transit_eq.js.html
function hashString(str) {
    code = 0;
    for (var i = 0; i < str.length; ++i) {
        code = 31 * code + str.charCodeAt(i);
        code %= 0x100000000;
    }
    return code;
};

//copy from http://cognitect.github.io/transit-js/files/src_com_cognitect_transit_eq.js.html
function hashCombine(seed, hash) {
    hash = hashString(hash);
    return seed ^ (hash + 0x9e3779b9 + (seed << 6) + (seed >> 2));
};

function compute_random_roll(checksum256_system,checksum160_user) {
    let hash = 0;
    const hash_sys = crypto.createHash('sha256');
    hash_sys.update(checksum256_system);
    checksum256_system_hex = hash_sys.digest('hex');
    hash =  hashCombine(hash,checksum256_system_hex);
    console.log('hash_sys=>'+hash);
    
    const hash_user = crypto.createHash('sha1');
    hash_user.update(checksum160_user);
    checksum256_user_hex = hash_user.digest('hex');
    hash =  hashCombine(hash,checksum256_user_hex);
    console.log('hash_user=>'+hash);

    return  hash % 100 +1;
}

var seed_sys  = '011adda82b2f1e36417b39363c098afc513e191286b48735f50432eb80659853';//ecc.sha256('60');
var seed_user  = '22f069f261c0fcffae034e2c2c12874580b7c8d4';//ecc.sha256('');
var result = compute_random_roll(seed_sys,seed_user);
console.log('result=>'+result);





// uint8_t compute_random_roll(const checksum256& seed1, const checksum160& seed2) {
//     size_t hash = 0;
//     hash_combine(hash, sha256_to_hex(seed1));
//     hash_combine(hash, sha1_to_hex(seed2));
//     return hash % 100 + 1;
// }


// // copied from boost https://www.boost.org/
// template <class T>
// inline void hash_combine(std::size_t& seed, const T& v) {
//     std::hash<T> hasher;
//     seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
// }
